package com.ttg.common.config;

import com.ttg.common.utils.sm.GmUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

@Configuration
public class DBPasswordDe implements BeanPostProcessor {

    private static final String DB_PREFIX ="spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties";
    private final boolean dbEn=true;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName){
        if(DB_PREFIX.equals(beanName)&&dbEn) {
            DataSourceProperties dataSourceProperties=(DataSourceProperties)bean;
            if(Objects.isNull(dataSourceProperties)|| StringUtils.isBlank(dataSourceProperties.getPassword())){
                return bean;
            }
            try {
                dataSourceProperties.setPassword(GmUtils.sm4ECBDecrypt(dataSourceProperties.getPassword()));
            } catch (Exception e) {
              e.printStackTrace();
            }
            return dataSourceProperties;
        }
        return bean;
    }

}